package com.banzhi.shop.entity.College;

import android.util.Log;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.videocollege.VideoCollegeCallBack;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2018/5/11.
 */

public class CollegeImpl implements IcollegeModel {
    @Override
    public void getData(final CollegeCallBack callBack) {
        RequestParams params = new RequestParams(URLString.CollegeURL);
        params.addBodyParameter("token","4770c2808f0d9a4d857f9b0f0671b63a");
       // params.addBodyParameter("token", "48949d92fdf6ab466939f56b04615e85");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("TAG", result.toString() );
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.optString("code").equals("0")) {
                        Gson gson = new Gson();
                        CollegeData collegeData = gson.fromJson(result, CollegeData.class);
                        callBack.ongetDataSuccess(collegeData);

                    }else {
                        callBack.ongetDataError(jsonObject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("CollegeImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(88)");

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                LoadingDialogUtils.dismiss();
            }
        });
    }
}
